﻿#include "UserRegisterCodec.h"

#include "UserRegisterCodec_Register.h"
#include "UserRegisterCodec_Unregister.h"

#include "qdebug.h"

UserRegisterCodec::UserRegisterCodec()
{
  setObjectName(getClassNameFirstLow<UserRegisterCodec>());
  codecMap.insert(RegisterType::Unregister,new UserRegisterCodec_Unregister());
  codecMap.insert(RegisterType::Register,new UserRegisterCodec_Register());
}

UserRegisterCodec::~UserRegisterCodec()
{
  for(unsigned char key:codecMap.keys())
    {
      delete codecMap.take(key);
    }
}

DataPackage_UserRegisterCodec* UserRegisterCodec::decode(DataPackage_UserRegisterCodec* dataPackage_UserRegisterCodec)
{
  QByteArray data=dataPackage_UserRegisterCodec->getData();
  if(data.isEmpty())
    {
      return Q_NULLPTR;
    }
  else
    {
      uchar dataType=data[0];
      dataPackage_UserRegisterCodec->setData(data.mid(1));
      if(RegisterType::Size<=dataType)
        {
          return Q_NULLPTR;
        }
      else
        {
          return codecMap.value(dataType)->decode(dataPackage_UserRegisterCodec);
        }
    }
}

DataPackage_UserRegisterCodec* UserRegisterCodec::makeAck(DataPackage_UserRegisterCodec* dataPackage_UserRegisterCodec,RegisterType registerType)
{
  return dataPackage_UserRegisterCodec->setData(QByteArray(registerType&0xff,1));
}

void UserRegisterCodec::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  for(unsigned char key:codecMap.keys())
    {
      ObjAutoInject* temp=dynamic_cast<ObjAutoInject*>(codecMap.value(key));
      if(temp)
        {
          temp->objectAutoInjection(objMap);
        }
    }
}
